package com.haiyou.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.ComponentScan;

import com.haiyou.common.constants.ServerConfigConstants;
import com.haiyou.common.constants.ServerTypeConstants;
import com.haiyou.common.system.Properties;
import com.haiyou.common.system.SystemProperties;
import com.haiyou.common.util.string.StringFormatUitls;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
@EnableConfigServer
@ComponentScan(value = {
		"com.haiyou.common",
        "com.haiyou.config"})
public class ConfigApplication {

	public static String nodeType = ServerTypeConstants.server_config;
	
	public static void main(String[] args) {
		
		try {
			
			SystemProperties.load(SystemProperties.getResourcesAbsolutePath(ServerConfigConstants.spring_path,
					ServerConfigConstants.config_properties));
			
			setProperty();
			
			SpringApplication.run(ConfigApplication.class,args);
			
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error(" 服务器启动发生异常,停止服务器 ");
			System.exit(0);
		}
	}
	
	
	/**
	 * 设置一些类型
	 */
	public static void setProperty() {

		System.setProperty("spring.devtools.restart.enabled", "false");

		// 设置节点类型
		System.setProperty(ServerConfigConstants.server_node_type, nodeType);

		String nameFormat = "{}({})";

		String name = StringFormatUitls.format(nameFormat, nodeType, Properties.server_node_id());

		// 设置应用名称
		System.setProperty(ServerConfigConstants.spring_application_name, name);
	}
}
